﻿#define _CRT_SECURE_NO_WARNINGS//模拟实现atoi
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
int my_atoi(char* str)
{
	int flag = 1;
	int ret = 0;
	//跳过空白字符
	while (isspace(*str))
	{
		str++;
	}
	if (*str == '\0')
	{
		return 0;
	}
	//跳过正负号
	if (*str == '+')
	{
		str++;
	}
	else if (*str == '-')
	{
		flag = -1;
		str++;
	}
	//开始转换数字字符直到非数字字符
	while (isdigit(*str))
	{
		ret = ret * 10 + flag * (*str - '0');
		if ((ret > INT_MAX) || (ret < INT_MIN))
		{
			return 0;
		}
		str++;
	}
	//正常停止
	if (*str == '\0')
	{
		return ret;
	}
	else
	{
		//遇到非数字字符
		return ret;
	}
}
int main()
{
	char s[7] = "-123456";
	/*int ret = atoi(s);*/
	/*printf("%d\n", ret);*/
	int ret = my_atoi(s);
	printf("%d\n", ret);
	return 0;
}